import { Next } from '@icon-park/vue-next';
import { ElMessage } from 'element-plus';
import { createRouter, createWebHistory } from 'vue-router'
import routes from './routes'


const router = createRouter({
    history: createWebHistory(),
    // 滚动行为
    scrollBehavior: (to, from, savePosition) => {
        if (savePosition) {
            return savePosition
        } else return { top: 0 }
    },
    routes
});

// 前置路由守卫
router.beforeEach(async (to, from, next) => {


    // 未登录状态跳转到发现
    if (from.name == "index" && to.name == "home") {
        next();
    } else {
        // 判断有没有登录
        if (!localStorage.getItem('token') && to.name !== "index") {
            router.push('/')
            ElMessage.error("请先登录")
        } else next()
    }

})

// 后置路由守卫
router.afterEach((to, from) => {
})

// onBeforeRouteUpdate(() => {

// })

// onBeforeRouteLeave(() => {

// })


export default router
